class Solution:
    def sumSubarrayMins(self, arr: List[int]) -> int:
        MOD = 1000000007
        s = []
        result = 0
        n = len(arr)
        leftLen = [0] * n
        rightLen = [0] * n
        for i, num in enumerate(arr):
            while s:
                if arr[s[-1]] >= num:
                    s.pop()
                else:
                    break
            subLen = i + 1 if not s else i - s[-1]
            leftLen[i] = subLen
            s.append(i)
        s = []
        for i in range(n-1, -1, -1):
            while s:
                if arr[s[-1]] > arr[i]:
                    s.pop()
                else:
                    break
            subLen = n - i if not s else s[-1] - i
            rightLen[i] = subLen
            s.append(i)
        for i in range(n):
            result += arr[i] * leftLen[i] * rightLen[i]
            result %= MOD
        return result
